package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.Adverinfo;
import com.ruoyi.system.domain.Attachements;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IAdverinfoService;
import com.ruoyi.system.service.IAttachementsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 广告 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-16
 */
@Controller
@RequestMapping("/system/adverinfo2")
public class AdverinfoController2 extends BaseController
{
    private String prefix = "system/adverinfo2";
	
	@Autowired
	private IAdverinfoService adverinfoService;
	@Autowired
	private IAttachementsService attachementsService;

	@RequiresPermissions("system:adverinfo2:view")
	@GetMapping()
	public String adverinfo()
	{
	    return prefix + "/adverinfo";
	}
	
	/**
	 * 查询广告列表
	 */
	@RequiresPermissions("system:adverinfo2:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Adverinfo adverinfo)
	{
		startPage();
        List<Adverinfo> list = adverinfoService.selectAdverinfoList(adverinfo);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出广告列表
	 */
	@RequiresPermissions("system:adverinfo2:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Adverinfo adverinfo)
    {
    	List<Adverinfo> list = adverinfoService.selectAdverinfoList(adverinfo);
        ExcelUtil<Adverinfo> util = new ExcelUtil<Adverinfo>(Adverinfo.class);
        return util.exportExcel(list, "adverinfo");
    }
	
	/**
	 * 新增广告
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}

	/**
	 * 新增保存广告
	 */
	@RequiresPermissions("system:adverinfo2:add")
	@Log(title = "广告", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HttpServletRequest request, Adverinfo adverinfo, MultipartFile[] files)
	{
		if (files != null && files.length > 0) {
			for (MultipartFile file : files) {

				Map<String, Object> fileInfo = saveFile(request,file);
				if (fileInfo != null) {
					Attachements attachement = new Attachements();
					attachement.setBeizhu(adverinfo.getUid());
					SysUser user = getSysUser();
					attachement.setModifyId(String.valueOf(user.getUserId()));

					attachement.setStatus(0);
					attachement.setCreateId(String.valueOf(user.getUserId()));

					attachement.setMc(fileInfo.get("name").toString());
					attachement.setPath(fileInfo.get("path").toString());
					attachement.setSuffix(fileInfo.get("suffix").toString());
					attachement.setLen(fileInfo.get("size").toString());

					attachementsService.insertAttachements(attachement);

				}
			}
		}
		return toAjax(adverinfoService.insertAdverinfo(adverinfo));
	}

	/**
	 * 修改广告
	 */
	@GetMapping("/edit/{advertinfoId}")
	public String edit(@PathVariable("advertinfoId") Integer advertinfoId, ModelMap mmap)
	{
		Adverinfo adverinfo = adverinfoService.selectAdverinfoById(advertinfoId);
		Attachements attachement = new Attachements();
		attachement.setBeizhu(adverinfo.getUid());
		List<Attachements>attachements = attachementsService.selectAttachementsList(attachement);
		mmap.put("attachements",attachements);
		mmap.put("adverinfo", adverinfo);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存广告
	 */
	@RequiresPermissions("system:adverinfo2:edit")
	@Log(title = "广告", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(HttpServletRequest request,Adverinfo adverinfo, MultipartFile[] files)
	{
		if (files != null && files.length > 0) {
			for (MultipartFile file : files) {

				Map<String, Object> fileInfo = saveFile(request,file);
				if (fileInfo != null) {
					Attachements attachement = new Attachements();
					attachement.setBeizhu(adverinfo.getUid());
					SysUser user = getSysUser();
					attachement.setModifyId(String.valueOf(user.getUserId()));

					attachement.setStatus(0);
					attachement.setCreateId(String.valueOf(user.getUserId()));

					attachement.setMc(fileInfo.get("name").toString());
					attachement.setPath(fileInfo.get("path").toString());
					attachement.setSuffix(fileInfo.get("suffix").toString());
					attachement.setLen(fileInfo.get("size").toString());

					attachementsService.insertAttachements(attachement);

				}
			}
		}
		return toAjax(adverinfoService.updateAdverinfo(adverinfo));
	}
	
	/**
	 * 删除广告
	 */
	@RequiresPermissions("system:adverinfo2:remove")
	@Log(title = "广告", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(adverinfoService.deleteAdverinfoByIds(ids));
	}
	
}
